package com.example.yinhangmiaosha.service;

import com.example.yinhangmiaosha.util.CacheKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class TimeServiceImpl implements TimeService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean getTimeIsPermitted() {
        String startTimeKey = CacheKey.ACTIVITY_TIME.getKey()+"_"+"start";
        String endTimeKey = CacheKey.ACTIVITY_TIME.getKey()+"_"+"end";
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;
        try {
            startTime = dateFormat.parse(stringRedisTemplate.opsForValue().get(startTimeKey));
            endTime = dateFormat.parse(stringRedisTemplate.opsForValue().get(endTimeKey));
        } catch (Exception e) {
//            e.printStackTrace();
            return false;
        }
        Date nowTime = new Date();
        if(nowTime.after(startTime) && nowTime.before(endTime)) return true;
        else return false;
    }
}
